using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Wilco.SyntaxHighlighting;
using Wilco.Windows.SyntaxHighlighting;

namespace CodeSyntaxHighLighter
{
	public partial class OptionsForm : Form
	{
		IParser _parser;
		HighlighterBase _highLighter;

		public OptionsForm()
		{
			InitializeComponent();
			_parser = new RtfParser(_codeText);
			HighLighterUtils.FillInHighLighters(_imageComboBox);

			SetHighLighterByName("C#");
		}

		private void SetHighLighterByName(string name)
		{
			ImageComboItem lngItem;
			foreach (Object item in _imageComboBox.Items)
			{
				lngItem = (ImageComboItem)item;
				if (lngItem.Text == name)
				{
					_imageComboBox.SelectedItem = lngItem;
					_highLighter = Register.Instance.Highlighters[lngItem.Text];
					_highLighter.Parser = _parser;
					break;
				}
			}
		}



		public string CodeText
		{
			get { return _codeText.Text; }
			set { _codeText.Text = value; }
		}

		public string Language
		{
			get
			{
				return _highLighter.Name;
			}
			set
			{
				SetHighLighterByName(value);
			}
		}

		private void _codeText_TextChanged(object sender, EventArgs e)
		{
			if (this._codeText.EnablePaint)
			{
				this._codeText.EnablePaint = false;
				int start = this._codeText.SelectionStart, length = this._codeText.SelectionLength;
				this._codeText.SuspendLayout();
				this._codeText.Rtf = this._highLighter.Parse(this._codeText.Text);
				this._codeText.SelectionStart = start;
				this._codeText.SelectionLength = length;
				this._codeText.EnablePaint = true;
			}
		}

		private void OptionsForm_Load(object sender, EventArgs e)
		{
			_codeText_TextChanged(_codeText, EventArgs.Empty);
		}

		private void _imageComboBox_SelectionChangeCommitted(object sender, EventArgs e)
		{
			ImageComboItem item = (ImageComboItem)_imageComboBox.SelectedItem;
			_highLighter = Register.Instance.Highlighters[item.Text];
			_highLighter.Parser = _parser;
			_codeText_TextChanged(_codeText, EventArgs.Empty);
		}

		private void _okButton_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
	}
}